package us.codecraft.webmagic.util;


import us.codecraft.webmagic.vo.HongyeHouseVo;

import java.sql.*;
import java.text.SimpleDateFormat;

/**
 * @progam:hongye-house
 * @author:GJS
 * @Time:2022/4/21 16:31
 */
public class JdbcUtil {

    private static Connection getConn(){
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hongye-house?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "1234";
        java.sql.Connection connection= null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 添加到数据库
     */
    public static int insertHouse(HongyeHouseVo HongyeHouseVo) {

        long time = System.currentTimeMillis();

        Connection connection = getConn();
        int id = 0;
        String sql="INSERT INTO homey_house ( house_name, address, price, sort, update_time, STATUS )VALUES(?,?,?,?,?,?)";

        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, HongyeHouseVo.getHouseName());
            ps.setString(2, HongyeHouseVo.getAddress());
            ps.setDouble(3, HongyeHouseVo.getPrice());
            ps.setInt(4,1);
            ps.setDate(5, new Date(time));
            ps.setInt(6,1);
            int i = ps.executeUpdate();
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }
}
